package com.yaolisheng.testMain;

import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author : yaols
 * Desc:
 * Company: 神州数码信息系统有限公司
 * Date: ${YEAR}/${MONTH}/${DAY}/${TIME}
 */
public class ThreadMainTest0 {

  public static void main(String[] args) {//传入数组n

    List<String> strings = new ArrayList<>();
    for (int i = 0; i < 1000; i++) {
      strings.add(">>>" + i);
    }
    List<String> list = execTest(strings);
    System.out.println(list);

  }

  public static List<String> execTest(List<String> list) {
    List<String> stringList = new Vector<>();

    // 1. 创建线程池
    ExecutorService executorService = Executors.newFixedThreadPool(100);

    // 2. 提交任务，并调用join()阻塞等待所有任务执行完成
    CompletableFuture.allOf(list.stream().map(key -> CompletableFuture.runAsync(() -> {
      // 睡眠一秒，模仿处理过程
      try {
        Thread.sleep(1000L);
      } catch (InterruptedException e) {
      }
      System.out.println("结果" + key);
      stringList.add(key);
    }, executorService)).toArray(CompletableFuture[]::new)).join();
    executorService.shutdown();


    return stringList;
  }
}
